module WorkingHoursHelper
  
  # Closer link for closing working hour form
  def close_link(name)
    link_to_function name do |page|
      page["add-hours"].slide_up
    end
  end
  
  # Calculate sum of hours
  def calculate_hours(hours)
    hours.collect {|hour| ((hour.end_time - hour.start_time) / 60.minutes).to_f }.inject(0) {|sum, num| sum + num }
  end
  
  # Calculate hours including lounch brakes
  def calculate_hours_and_lounch_brake(hours, lounch_time)
    bt = lounch_time / 1.hour.to_f
    hours.collect {|hour| ((hour.end_time - hour.start_time) / 60.minutes - bt).to_f }.inject(0) {|sum, num| sum + num }
  end
  
  # Defines export button. Button is disabled when there is no hours
  def custom_submit_button(name, hours, options = {})
    submit_tag name, { :disabled => hours.empty? }.merge!(options)
  end
  
  # Link for destroying hour
  def destroy_hour_link(name, id)
    link_to name, user_working_hour_path(@user.id, id), :confirm => 'Are you sure?', :method => :delete
  end
  
  def remote_function_for(field)
    remote_function(:update => "calculate_hours",
                    :url => { :action => :update_hours })
  end
end